<?php
/**
 * PHP version 5
 * @copyright  Samsun Xu 2009
 * @package    Frontend
 * @license    LGPL
 * @get product list
 */
session_start();
require('system/initialize.php');

class getProductList
{
	public function __construct()
	{
		require_once('system/libraries/currency.php');
		$this->obj_currency = new Currency();
	}
	
	public function run()
	{
		$str  = $this->generateProductList();
		echo $str;
	}
	
	public function getProductList()
	{
		$perPageRecord  = $this->setPerPageRecord();
		$pageNum		= $this->setPageNum();
		$colours		= $this->getColour();
		//$sizes		= $this->getSize();		
		
		$this->setDB();
		
		$productType = (isset($_SESSION['productType'])) ? $_SESSION['productType'] : "";
		
		switch($productType){
			case "hot":
				$condition = " where hot=1 and published=1 ";
				break;
			case "new":
				$condition = " where new=1 and price<50 and published=1 ";
				break;
			case "xxxxl":
				$condition = " where xxxxl=1 and published=1 ";
				break;
			case "sales":
				$condition = " where sales=1 and published=1 ";
				break;
			case "all":
				$condition = " where published=1 ";
				break;
			default:			
				$condition = " where hot=1 and published=1 ";
				break;		
		}

		// Category Or Subcategory
		if(isset($_GET['category']) && $_GET['category'] != "") {
			$sql 		= "SELECT pid FROM tl_taxonomy where id=".$_GET['category'];
			$result 	= @mysql_query($sql);
			$countRd 	= @mysql_fetch_assoc($result);
			if($countRd['pid']==0){
				$condition .= " And category IN ( SELECT id FROM `tl_taxonomy` WHERE pid =".$_GET['category'].") ";
			}else{
				$condition .= " And category=".$_GET['category'];
			}
		}
		
		// Brand
		if(isset($_GET['brand']) && $_GET['brand'] != "") $condition .= " And brand=".$_GET['brand'];


		$fromRecord = ($pageNum-1)*$perPageRecord;
		$sql		= "SELECT * FROM `product_catalog` $condition ORDER BY sorting ASC LIMIT ".$fromRecord.", ".$perPageRecord;
		
		
		// Tags
		if (isset($_GET['tag']) && $_GET['tag']!="")
		{
			switch($_SESSION['productType']){
				case "hot":
					$condition = " where `product_catalog`.hot=1 and `product_catalog`.published=1 ";
					break;
				case "new":
					$condition = " where `product_catalog`.new=1 and `product_catalog`.price<50 and `product_catalog`.published=1 ";
					break;
				case "xxxxl":
					$condition = " where `product_catalog`.xxxxl=1 and `product_catalog`.published=1 ";
					break;
				case "sales":
					$condition = " where `product_catalog`.sales=1 and `product_catalog`.published=1 ";
					break;
				case "all":
					$condition = " where `product_catalog`.published=1 ";
					break;
				default:			
					$condition = " where `product_catalog`.hot=1 and `product_catalog`.published=1 ";
					break;		
			}
			$sql = "SELECT `product_catalog`.* FROM `catalog_tags` LEFT JOIN `product_catalog` ON `product_catalog`.`tags` LIKE CONCAT('%,', `catalog_tags`.`id`, '') OR `product_catalog`.`tags` LIKE CONCAT('', `catalog_tags`.`id`, ',%') OR `product_catalog`.`tags` LIKE CONCAT('%,', `catalog_tags`.`id`, ',%') OR `product_catalog`.`tags` LIKE `catalog_tags`.`id`
			$condition and `catalog_tags`.`id`=".$_GET['tag']."  ORDER BY `product_catalog`.sorting ASC LIMIT ".$fromRecord.", ".$perPageRecord;
		}		

		$result 	= @mysql_query($sql);
		
		$str		= "<ul>";
		$k=0;
		while ($objRd = @mysql_fetch_assoc($result))
		{
			$productURL = "product.html?category=".$objRd['category']."&id=".$objRd['id'];
			$imageStr = (file_exists("daft_files/catalog/products/" . $objRd['skuid'] . "/thumb.jpg")) ? "daft_files/catalog/products/" . $objRd['skuid'] . "/thumb.jpg" : "image.php?src=" . $objRd['img'] . "&width=155&height=145";
			$str .= "<li>";
			$str .= "<a href=\"$productURL\" title=\"".$objRd['name']."\" class=\"thumb\"><img src=\"".$imageStr . "\" alt=\"".$objRd['name']."\" /></a>";
			$str .= "<h3><a href=\"$productURL\" title=\"".$objRd['name']."\">".$objRd['name']."</a></h3>";
			
			// get Colour
			$sql = "SELECT * FROM `tl_catalog_attribute` WHERE `id_catalog`='" . $objRd['id'] . "' GROUP BY `id_color`";
			$res = @mysql_query($sql);
			$str .= "<p class=\"color\">";
			while ($objAttr = @mysql_fetch_assoc($res))
			{
				$str .= "<a href=\"$productURL&id_colour=" . $objAttr['id_color'] . "#product_detail\">";
				$str .= "<img src=\"".$colours[$objAttr['id_color']]['img']."\" alt=\"".$colours[$objAttr['id_color']]['title']."\" />";
				$str .= "</a>";
			}
			$str .= "</p>";			
			
			// get Price
			$str .= "<p class=\"price\"><em>".$this->obj_currency->change_order($objRd['price']).(($objRd['sales']) ? '&nbsp;<span style="color:#999; font-size:11px;">' . $this->obj_currency->change_iso_order($objRd['wasprice']) : '') . "</span></em></p>";
			$str .= "</li>";
			$k++;
		}
		$str	 .= "</ul>";
		$productArray = array($k, $str);
		return $productArray;
	}

	public function getSearchResult()
	{		
		$perPageRecord  = $this->setPerPageRecord();
		$pageNum		= $this->setPageNum();
		$colours		= $this->getColour();

		$condition 		= $_SESSION['searchCondition']['session_cond'];
		$condition_together 		= $_SESSION['searchCondition']['session_cond_together'];
		
		// Start Search
		$fromRecord = ($pageNum-1)*$perPageRecord;
		/*$sql		= "SELECT product_catalog.*, catalog_tags.tag, catalog_colour.title
						FROM `product_catalog` 
						LEFT JOIN `catalog_tags` ON `product_catalog`.`tags` LIKE CONCAT('%,', `catalog_tags`.`id`, '') OR `product_catalog`.`tags` LIKE CONCAT('', `catalog_tags`.`id`, ',%') OR `product_catalog`.`tags` LIKE CONCAT('%,', `catalog_tags`.`id`, ',%') OR `product_catalog`.`tags` LIKE `catalog_tags`.`id` 
                        LEFT JOIN `catalog_colour` ON `catalog_colour`.`id` IN (`product_catalog`.`colour`)
						$condition GROUP BY `product_catalog`.`id` ORDER BY product_catalog.sorting ASC LIMIT ".$fromRecord.", ".$perPageRecord;*/
		$sql		= "(SELECT product_catalog.*, catalog_tags.tag, catalog_colour.title,`catalog_size`.size
						FROM `product_catalog` 
						LEFT JOIN `catalog_tags` ON `product_catalog`.`tags` LIKE CONCAT('%,', `catalog_tags`.`id`, '') OR `product_catalog`.`tags` LIKE CONCAT('', `catalog_tags`.`id`, ',%') OR `product_catalog`.`tags` LIKE CONCAT('%,', `catalog_tags`.`id`, ',%') OR `product_catalog`.`tags` LIKE `catalog_tags`.`id`
						LEFT JOIN `tl_catalog_attribute` ON `tl_catalog_attribute`.`id_catalog`=product_catalog.`id`
                        LEFT JOIN `catalog_colour` ON `catalog_colour`.`id`=`tl_catalog_attribute`.`id_color`
						LEFT JOIN `catalog_size` ON `catalog_size`.`id`=`tl_catalog_attribute`.`id_size` 
						$condition_together GROUP BY `tl_catalog_attribute`.`id_catalog` ORDER BY product_catalog.sorting ASC) UNION (SELECT product_catalog.*, catalog_tags.tag, catalog_colour.title,`catalog_size`.size
						FROM `product_catalog` 
						LEFT JOIN `catalog_tags` ON `product_catalog`.`tags` LIKE CONCAT('%,', `catalog_tags`.`id`, '') OR `product_catalog`.`tags` LIKE CONCAT('', `catalog_tags`.`id`, ',%') OR `product_catalog`.`tags` LIKE CONCAT('%,', `catalog_tags`.`id`, ',%') OR `product_catalog`.`tags` LIKE `catalog_tags`.`id`
						LEFT JOIN `tl_catalog_attribute` ON `tl_catalog_attribute`.`id_catalog`=product_catalog.`id`
                        LEFT JOIN `catalog_colour` ON `catalog_colour`.`id`=`tl_catalog_attribute`.`id_color`
						LEFT JOIN `catalog_size` ON `catalog_size`.`id`=`tl_catalog_attribute`.`id_size` 
						$condition GROUP BY `tl_catalog_attribute`.`id_catalog` ORDER BY product_catalog.sorting ASC) LIMIT ".$fromRecord.", ".$perPageRecord;

		$result 	= @mysql_query($sql);
		
		$str		= "<ul>";
		$k=0;
		while ($objRd = @mysql_fetch_assoc($result))
		{
			$productURL = "product.html?category=".$objRd['category']."&id=".$objRd['id'];
			$imageStr = (file_exists("daft_files/catalog/products/" . $objRd['skuid'] . "/thumb.jpg")) ? "daft_files/catalog/products/" . $objRd['skuid'] . "/thumb.jpg" : "image.php?src=" . $objRd['img'] . "&width=155&height=145";
			$str .= "<li>";
			$str .= "<a href=\"$productURL\" title=\"".$objRd['name']."\" class=\"thumb\"><img src=\"".$imageStr."\" /></a>";
			$str .= "<h3><a href=\"$productURL\" title=\"".$objRd['name']."\">".$objRd['name']."</a></h3>";
			
			// get Colour
			$sql = "SELECT * FROM `tl_catalog_attribute` WHERE `id_catalog`='" . $objRd['id'] . "' GROUP BY `id_color`";
			$res = @mysql_query($sql);
			$str .= "<p class=\"color\">";
			while ($objAttr = @mysql_fetch_assoc($res))
			{
				$str .= "<a href=\"$productURL&id_colour=" . $objAttr['id_color'] . "#product_detail\">";
				$str .= "<img src=\"".$colours[$objAttr['id_color']]['img']."\" alt=\"".$colours[$objAttr['id_color']]['title']."\" />";
				$str .= "</a>";
			}
			$str .= "</p>";	
			
			// get Price
			$str .= "<p class=\"price\"><em>".$this->obj_currency->change_order($objRd['price']).(($objRd['sales']) ? '&nbsp;<span style="color:#999; font-size:11px;">' . $this->obj_currency->change_iso_order($objRd['wasprice']) : '') . "</span></em></p>";
			$str .= "</li>";
			$k++;
		}
		$str	 .= "</ul>";
		$productArray = array($k, $str);
		return $productArray;
	}
	
	public function getColour()
	{
		$dataArray = array();
		$this->setDB();
		$sql	= "SELECT * FROM `catalog_colour` Order by id asc";
		$result = @mysql_query($sql);

		while ($objRd = @mysql_fetch_assoc($result))
		{
			$dataArray[$objRd['id']]['title'] = $objRd['title'];
			$dataArray[$objRd['id']]['img']   = $objRd['img'];
		}
		
		return $dataArray;
	}

	public function setPerPageRecord()
	{
		if($_SESSION['perPageRecord'] == ""){
			$_SESSION['perPageRecord'] = 8;
		}else{
			if(isset($_GET['perPageRecord']) && $_GET['perPageRecord'] != "") $_SESSION['perPageRecord'] = $_GET['perPageRecord'];
		}
		return $_SESSION['perPageRecord'];
	}
	
	public function setPageNum()
	{
		if($_GET['pageNum'] == "") {
			$pageNum = 1;
		}else{
			$pageNum = $_GET['pageNum'];
		}
		return $pageNum;
	}

	public function setDB()
	{
		$strHost = $GLOBALS['TL_CONFIG']['dbHost'];
		if($GLOBALS['TL_CONFIG']['dbPort']){
			$strHost .= ':' . $GLOBALS['TL_CONFIG']['dbPort'];
		}

		if($GLOBALS['TL_CONFIG']['dbPconnect']){
			$resConnection = @mysql_pconnect($strHost, $GLOBALS['TL_CONFIG']['dbUser'], $GLOBALS['TL_CONFIG']['dbPass']);
		}else{
			$resConnection = @mysql_connect($strHost, $GLOBALS['TL_CONFIG']['dbUser'], $GLOBALS['TL_CONFIG']['dbPass']);
		}

		if (is_resource($resConnection)){
			@mysql_query("SET sql_mode=''", $resConnection);
			@mysql_query("SET NAMES " . $GLOBALS['TL_CONFIG']['dbCharset'], $resConnection);
			@mysql_select_db($GLOBALS['TL_CONFIG']['dbDatabase'], $resConnection);
		}
	}	

	public function setListView()
	{
		$listView8 	= 8;
		$listView24	= 24;
		$currentURL = $_SERVER["REQUEST_URI"];
		if(isset($_GET['perPageRecord']) && $_GET['perPageRecord'] != ""){
			$currentURL = str_replace("?perPageRecord=".$_GET['perPageRecord'], "", $currentURL);
			$currentURL = str_replace("&perPageRecord=".$_GET['perPageRecord'], "", $currentURL);
		}

		$pos = strpos($currentURL, "?");

		if($pos == true){
			$phpFile	= $currentURL."&perPageRecord=";
		}else{
			$phpFile	= $currentURL."?perPageRecord=";
		}

		if($_SESSION['perPageRecord'] == 24){
			$listViewHtml = "<a href=\"".$phpFile.$listView8."\">View ".$listView8." Items Per Page</a> |";
		}else{
			$listViewHtml = "<a href=\"".$phpFile.$listView24."\">View ".$listView24." Items Per Page</a> |";
		}

		return $listViewHtml;
	
	}
	
	public function generateProductList()
	{
		$listView		= $this->setListView();
		if(isset($_GET['action']) && ($_GET['action'] == 1)) {$search_action="action";}else{$search_action="";}

		if($search_action == "action"){
			$productList	= $this->getSearchResult();
		}else{
			$productList	= $this->getProductList();
		}

		$perPageRecord  = $this->setPerPageRecord();
		if($productList[0]==0){
			$strHtml = "";
		}else{
			$strHtml 		= 	$productList[1];
		}

		return $strHtml;
	}
	
}

$objGetRd = new getProductList();
$objGetRd->run();

?>